using System;
using System.IO;
using System.Text;

namespace SharePoint.ImportExport.Core
{
	/// <summary>
	/// This enum controls what will be logged
	/// </summary>
	public enum ErrorLevel
	{
		/// <summary>
		/// This information will not be logged
		/// </summary>
		NoLog=-2,
		/// <summary>
		/// The information will be logged to the log file
		/// </summary>
		File=-1,
		/// <summary>
		/// Informative log info, not an error
		/// </summary>
		Information=0,
		/// <summary>
		/// Logged as a warning
		/// </summary>
		Warning=1,
		/// <summary>
		/// Logged as an error
		/// </summary>
		Error=2,
		/// <summary>
		/// Progress information written to the UI
		/// </summary>
		Progress=3
		
	}
	// FireEventArgs: a custom event inherited from EventArgs.

	/// <summary>
	/// This is the SPEventArgs class
	/// </summary>
	public class SPEventArgs: EventArgs 
	{
		/// <summary>
		/// This is the default constructor for the class
		/// </summary>
		/// <param name="level">This is the error level that functions as a threshhold for the logging</param>
		/// <param name="message">This is the message that will be logged</param>
		public SPEventArgs(ErrorLevel level, string message) 
		{
			this.level=level;
			this.message= message;
		}

		private ErrorLevel level;
		private string message;

		/// <summary>
		/// This is the ErrorLevel
		/// </summary>
		public ErrorLevel Level
		{
			get
			{
				return level;
			
			}
		}
		/// <summary>
		/// This is the error message
		/// </summary>
		public string Message
		{
			get
			{
				return message;
			
			}
		}

	}    //end of class FireEventArgs

	/// <summary>
	/// This is the helper class for logging
	/// </summary>
	public class Logger
	{
		/// <summary>
		/// 
		/// </summary>
		public delegate void LogDataEventHandler(object sender, SPEventArgs e);
		/// <summary>
		/// 
		/// </summary>
		public static event LogDataEventHandler LogDataSendingEvent=null;
		private static TextWriter logFile=null;
		private static bool bInit=false;
		private static string InternalLastError="";
		private static string LogFileName="";
		private static ErrorLevel ReportLevel=ErrorLevel.Information;
		/// <summary>
		/// Default constructor
		/// </summary>
		public Logger()
		{
		}
		/// <summary>
		/// Default destructor
		/// </summary>
		~ Logger() 
		{
			if(null !=logFile)
			{
				logFile.Flush();
				logFile.Close();
			}
		}
		/// <summary>
		/// 
		/// </summary>
		public static void Close()
		{
			try
			{
				logFile.Flush();
			
			}
			finally
			{
				if(null !=logFile)
					logFile.Close();
				logFile=null;
			}
		}

		/// <summary>
		/// This method writes a string to the logfile
		/// </summary>
		/// <param name="level">This is the errorlevel threshold. The error will only be logged if it has a higher value than the reporting threshold</param>
		/// <param name="logInfo">This is the logged error message</param>
		public static void Log(ErrorLevel level, string logInfo)
		{
			if(level==ErrorLevel.NoLog) //Dont do anything
				return;
			if(bInit)
			{
				logFile.WriteLine(logInfo);
				logFile.Flush();
			}
			//Report to the screen only those errors that pass the threshold
			if(level>=ReportLevel)
			{
				if(LogDataSendingEvent != null)
				{
					SPEventArgs args=new SPEventArgs(level,logInfo);
					LogDataSendingEvent(null,args);
				}

			}
			System.Diagnostics.Debug.WriteLine(logInfo);
			InternalLastError=logInfo;
		}
		/// <summary>
		/// This method initializes the logfile specified by the parameter
		/// </summary>
		/// <param name="inFileName">This is the filename</param>
		/// <param name="level">This is the errorlevel for the reporting</param>
		public static void Init(string inFileName, ErrorLevel level)
		{
			try
			{
				LogFileName=inFileName;
				logFile=File.CreateText(inFileName);
				bInit=true;
				ReportLevel=level;
			}
			catch(IOException Ex)
			{
				System.Diagnostics.Debug.WriteLine("Could not open the file: "+inFileName+". Error: "+Ex.Message);
			}
		}

		/// <summary>
		/// This is the last logged error
		/// </summary>
		public static string LastError
		{
			get
			{
				return InternalLastError;
			}
		}

		/// <summary>
		/// This is the filename of the log file
		/// </summary>
		public static string FileName
		{
			get
			{
				return LogFileName;
			}
		}
	}
}
